home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / PPC / PPCUnARJ / source / unarj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-07  |  22.8 KB  |  1,022 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 02/17/93 (Amiga Port 03/18/95)
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991-93 by Robert K Jung.  All rights reserved.
  4.  * Amiga Port done by Andreas R. Kleinert in 1995-97, powerUP (TM) in 1998
  5.  *
  6.  *   This code may be freely used in programs that are NOT ARJ archivers
  7.  *   (both compress and extract ARJ archives).
  8.  *
  9.  *   If you wish to distribute a modified version of this program, you
  10.  *   MUST indicate that it is a modified version both in the program and
  11.  *   source code.
  12.  *
  13.  *   I am holding the copyright on the source code, so please do not
  14.  *   delete my name from the program files or from the documentation.
  15.  *
  16.  *   I wish to give credit to Haruhiko Okumura for providing the
  17.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  18.  *   that UNARJ is significantly different from AR from an archive
  19.  *   structural point of view.
  20.  *
  21.  * Modification history:
  22.  * Date      Programmer  Description of modification.
  23.  * 04/05/91  R. Jung     Rewrote code.
  24.  * 04/23/91  M. Adler    Portabilized.
  25.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  26.  *                       fillbuf().
  27.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  28.  * 05/25/91  R. Jung     Improved find_header().
  29.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  30.  *                       set_ftime_mode().
  31.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  32.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  33.  *                       Added strlower().
  34.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  35.  * 07/21/91  R. Jung     Added #ifdef VMS.
  36.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  37.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  38.  *                       per Eric Larson.
  39.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  40.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  41.  * 11/24/91  R. Jung     Added more error_count processing.
  42.  * 12/03/91  R. Jung     Added backup file processing.
  43.  * 02/17/93  R. Jung     Added archive modified date support.
  44.  * 03/18/95  A. Kleinert Ported V2.41 to Amiga. Did some necessary changes.
  45.  * 10/27/97  A. Kleinert recompiled with SAS/C 6.58
  46.  * 02/07/98  A. Kleinert ported to powerUP (TM), using SAS/C for PPC
  47.  *
  48.  */
  49.  
  50. #include "unarj.h"
  51.  
  52. #ifdef MODERN
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <ctype.h>
  56. #else /* !MODERN */
  57. extern void free();
  58. extern void exit();
  59. extern char *strcat();
  60. extern char *strcpy();
  61. extern char *strncpy();
  62. extern char *strchr();
  63. extern char *strrchr();
  64. extern int strlen();
  65. extern int strcmp();
  66. #ifdef VMS
  67. #include <ssdef.h>
  68. #define EXIT_FAILURE SS$_ABORT
  69. #define EXIT_SUCCESS SS$_NORMAL
  70. #else
  71. #define EXIT_FAILURE (1)
  72. #define EXIT_SUCCESS (0)
  73. #endif
  74. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  75. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  76. #endif /* ?MODERN */
  77.  
  78. #ifdef THINK_C
  79. #include <console.h>
  80. #endif
  81.  
  82. /* Global variables */
  83.  
  84. UCRC   crc;
  85. FILE   *arcfile;
  86. FILE   *outfile;
  87. ushort bitbuf;
  88. long   compsize;
  89. long   origsize;
  90. uchar  subbitbuf;
  91. uchar  header[HEADERSIZE_MAX];
  92. char   arc_name[FNAME_MAX];
  93. int    command;
  94. int    bitcount;
  95. int    file_type;
  96. int    no_output;
  97. int    error_count;
  98.  
  99. /* Messages */
  100.  
  101. static char *M_USAGE  [] =
  102. {
  103. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  104. "        UNARJ e archive        (extract archive)\n",
  105. "        UNARJ l archive        (list archive)\n",
  106. "        UNARJ t archive        (test archive)\n",
  107. "        UNARJ x archive        (extract with pathnames)\n",
  108. "\n",
  109. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  110. "You may freely use, copy and distribute this program, provided that no fee\n",
  111. "is charged for such use, copying or distribution, and it is distributed\n",
  112. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  113. "warranty of any kind, express or implied, including but not limited to\n",
  114. "the implied warranties of merchantability and fitness for a particular\n",
  115. "purpose.  Refer to UNARJ.DOC for more warranty information.\n",
  116. "\n",
  117. "ARJ Software                    Internet address :  robjung@world.std.com\n",
  118. "Robert and Susan Jung           CompuServe userid:  72077,445\n",
  119. "2606 Village Road West\n",
  120. "Norwood, Massachusetts 02062\n",
  121. "USA\n",
  122. "\npowerUP (TM) port has been done by Andreas R. Kleinert",
  123. "\nEMail: Andreas_Kleinert@t-online.de\n\n",
  124. NULL
  125. };
  126.  
  127. char M_VERSION [] = "UNARJ (Demo version) 2.41 Copyright (c) 1991-93 Robert K Jung\n"
  128.                     "powerUP (TM) Port V1.02 of UnARJ 2.41, done by Andreas R. Kleinert in 1998\n\n";
  129.  
  130. char M_ARCDATE [] = "Archive created: %s";
  131. char M_ARCDATEM[] = ", modified: %s";
  132. char M_BADCOMND[] = "Bad UNARJ command: %s";
  133. char M_BADCOMNT[] = "Invalid comment header";
  134. char M_BADHEADR[] = "Bad header";
  135. char M_BADTABLE[] = "Bad Huffman code";
  136. char M_CANTOPEN[] = "Can't open %s";
  137. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  138. char M_CANTWRIT[] = "Can't write file. Disk full?";
  139. char M_CRCERROR[] = "CRC error!\n";
  140. char M_CRCOK   [] = "CRC OK\n";
  141. char M_DIFFHOST[] = "  Binary file!";
  142. char M_ENCRYPT [] = "File is password encrypted, ";
  143. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  144. char M_EXTRACT [] = "Extracting %-25s";
  145. char M_FEXISTS [] = "%-25s exists, ";
  146. char M_HEADRCRC[] = "Header CRC error!";
  147. char M_NBRFILES[] = "%5d file(s)\n";
  148. char M_NOMEMORY[] = "Out of memory";
  149. char M_NOTARJ  [] = "%s is not an ARJ archive";
  150. char M_PROCARC [] = "Processing archive: %s\n";
  151. char M_SKIPPED [] = "Skipped %s\n";
  152. char M_SUFFIX  [] = ARJ_SUFFIX;
  153. char M_TESTING [] = "Testing    %-25s";
  154. char M_UNKNMETH[] = "Unsupported method: %d, ";
  155. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  156. char M_UNKNVERS[] = "Unsupported version: %d, ";
  157.  
  158. #define get_crc()       get_longword()
  159. #define fget_crc(f)     fget_longword(f)
  160.  
  161. #define setup_get(PTR)  (get_ptr = (PTR))
  162. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  163.  
  164. #define BUFFERSIZE      4096
  165.  
  166. #define ASCII_MASK      0x7F
  167.  
  168. #define CRCPOLY         0xEDB88320L
  169.  
  170. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  171.  
  172. /* Local functions */
  173.  
  174. #ifdef MODERN
  175. static void  make_crctable(void);
  176. static void  crc_buf(char *str, int len);
  177. static void  strparity(uchar *p);
  178. static FILE  *fopen_msg(char *name, char *mode);
  179. static int   fget_byte(FILE *f);
  180. static uint  fget_word(FILE *f);
  181. static ulong fget_longword(FILE *f);
  182. static void  fread_crc(uchar *p, int n, FILE *f);
  183. static void  decode_path(char *name);
  184. static void  get_date_str(char *str, ulong tstamp);
  185. static int   parse_path(char *pathname, char *path, char *entry);
  186. static void  strncopy(char *to, char *from, int len);
  187. static uint  get_word(void);
  188. static ulong get_longword(void);
  189. static long  find_header(FILE *fd);
  190. static int   read_header(int first, FILE *fd, char *name);
  191. static void  skip(void);
  192. static void  unstore(void);
  193. static int   check_flags(void);
  194. static int   extract(void);
  195. static int   test(void);
  196. static uint  ratio(long a, long b);
  197. static void  list_start(void);
  198. static void  list_arc(int count);
  199. static void  execute_cmd(void);
  200. static void  help(void);
  201. #endif /* MODERN */
  202.  
  203. /* Local variables */
  204.  
  205. static char   filename[FNAME_MAX];
  206. static char   comment[COMMENT_MAX];
  207. static char   *hdr_filename;
  208. static char   *hdr_comment;
  209.  
  210. static ushort headersize;
  211. static uchar  first_hdr_size;
  212. static uchar  arj_nbr;
  213. static uchar  arj_x_nbr;
  214. static uchar  host_os;
  215. static uchar  arj_flags;
  216. static short  method;
  217. static uint   file_mode;
  218. static ulong  time_stamp;
  219. static short  entry_pos;
  220. static ushort host_data;
  221. static uchar  *get_ptr;
  222. static UCRC   file_crc;
  223. static UCRC   header_crc;
  224.  
  225. static long   first_hdr_pos;
  226. static long   torigsize;
  227. static long   tcompsize;
  228.  
  229. static int    clock_inx;
  230.  
  231. static char   *writemode[2]  = { "wb",  "w" };
  232.  
  233. static UCRC   crctable[UCHAR_MAX + 1];
  234.  
  235. /* Functions */
  236.  
  237. static void
  238. make_crctable()
  239. {
  240.     uint i, j;
  241.     UCRC r;
  242.  
  243.     for (i = 0; i <= UCHAR_MAX; i++)
  244.     {
  245.         r = i;
  246.         for (j = CHAR_BIT; j > 0; j--)
  247.         {
  248.             if (r & 1)
  249.                 r = (r >> 1) ^ CRCPOLY;
  250.             else
  251.                 r >>= 1;
  252.         }
  253.         crctable[i] = r;
  254.     }
  255. }
  256.  
  257. static void
  258. crc_buf(str, len)
  259. char *str;
  260. int  len;
  261. {
  262.     while (len--)
  263.         UPDATE_CRC(crc, *str++);
  264. }
  265.  
  266. void
  267. disp_clock()
  268. {
  269.     static char clock_str[4] = { '|', '/', '-', '\\' };
  270.  
  271.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  272.     clock_inx = (clock_inx + 1) & 0x03;
  273. }
  274.  
  275. void
  276. error(fmt, arg)
  277. char *fmt;
  278. char *arg;
  279. {
  280.     putc('\n', stdout);
  281.     printf(fmt, arg, error_count);
  282.     putc('\n', stdout);
  283.     exit(EXIT_FAILURE);
  284. }
  285.  
  286. static void
  287. strparity(p)
  288. uchar *p;
  289. {
  290.     while (*p)
  291.     {
  292.         FIX_PARITY(*p);
  293.         p++;
  294.     }
  295. }
  296.  
  297. static FILE *
  298. fopen_msg(name, mode)
  299. char *name;
  300. char *mode;
  301. {
  302.     FILE *fd;
  303.  
  304.     fd = file_open(name, mode);
  305.     if (fd == NULL)
  306.         error(M_CANTOPEN, name);
  307.     return fd;
  308. }
  309.  
  310. static int
  311. fget_byte(f)
  312. FILE *f;
  313. {
  314.     int c;
  315.  
  316.     if ((c = getc(f)) == EOF)
  317.         error(M_CANTREAD, "");
  318.     return c & 0xFF;
  319. }
  320.  
  321. static uint
  322. fget_word(f)
  323. FILE *f;
  324. {
  325.     uint b0, b1;
  326.  
  327.     b0 = fget_byte(f);
  328.     b1 = fget_byte(f);
  329.     return (b1 << 8) + b0;
  330. }
  331.  
  332. static ulong
  333. fget_longword(f)
  334. FILE *f;
  335. {
  336.     ulong b0, b1, b2, b3;
  337.  
  338.     b0 = fget_byte(f);
  339.     b1 = fget_byte(f);
  340.     b2 = fget_byte(f);
  341.     b3 = fget_byte(f);
  342.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  343. }
  344.  
  345. static void
  346. fread_crc(p, n, f)
  347. uchar *p;
  348. int   n;
  349. FILE  *f;
  350. {
  351.     n = file_read((char *)p, 1, n, f);
  352.     origsize += n;
  353.     crc_buf((char *)p, n);
  354. }
  355.  
  356. void
  357. fwrite_txt_crc(p, n)
  358. uchar *p;
  359. int   n;
  360. {
  361.     uchar c;
  362.  
  363.     crc_buf((char *)p, n);
  364.     if (no_output)
  365.         return;
  366.  
  367.     if (file_type == TEXT_TYPE)
  368.     {
  369.         while (n--)
  370.         {
  371.             c = *p++;
  372.             if (host_os != OS)
  373.             {
  374.                 FIX_PARITY(c);
  375.             }
  376.             if (putc((int) c, outfile) == EOF)
  377.                 error(M_CANTWRIT, "");
  378.         }
  379.     }
  380.     else
  381.     {
  382.         if (file_write((char *)p, 1, n, outfile) != n)
  383.             error(M_CANTWRIT, "");
  384.     }
  385. }
  386.  
  387. void
  388. init_getbits()
  389. {
  390.     bitbuf = 0;
  391.     subbitbuf = 0;
  392.     bitcount = 0;
  393.     fillbuf(2 * CHAR_BIT);
  394. }
  395.  
  396. void
  397. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  398. int n;
  399. {
  400.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  401.     while (n > bitcount)
  402.     {
  403.         bitbuf |= subbitbuf << (n -= bitcount);
  404.         if (compsize != 0)
  405.         {
  406.             compsize--;
  407.             subbitbuf = (uchar) getc(arcfile);
  408.         }
  409.         else
  410.             subbitbuf = 0;
  411.         bitcount = CHAR_BIT;
  412.     }
  413.     bitbuf |= subbitbuf >> (bitcount -= n);
  414. }
  415.  
  416. ushort
  417. getbits(n)
  418. int n;
  419. {
  420.     ushort x;
  421.  
  422.     x = bitbuf >> (2 * CHAR_BIT - n);
  423.     fillbuf(n);
  424.     return x;
  425. }
  426.  
  427. static void
  428. decode_path(name)
  429. char *name;
  430. {
  431.     for ( ; *name; name++)
  432.     {
  433.         if (*name == ARJ_PATH_CHAR)
  434.             *name = PATH_CHAR;
  435.     }
  436. }
  437.  
  438. static void
  439. get_date_str(str, tstamp)
  440. char  *str;
  441. ulong tstamp;
  442. {
  443.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  444.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  445.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  446. }
  447.  
  448. static int
  449. parse_path(pathname, path, entry)
  450. char *pathname;
  451. char *path;
  452. char *entry;
  453. {
  454.     char *cptr, *ptr, *fptr;
  455.     short pos;
  456.  
  457.     fptr = NULL;
  458.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  459.     {
  460.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  461.                 (fptr == NULL || ptr > fptr))
  462.             fptr = ptr;
  463.     }
  464.     if (fptr == NULL)
  465.         pos = 0;
  466.     else
  467.         pos = fptr + 1 - pathname;
  468.     if (path != NULL)
  469.     {
  470.        strncpy(path, pathname, pos);
  471.        path[pos] = NULL_CHAR;
  472.     }
  473.     if (entry != NULL)
  474.        strcpy(entry, &pathname[pos]);
  475.     return pos;
  476. }
  477.  
  478. static void
  479. strncopy(to, from, len)
  480. char *to;
  481. char *from;
  482. int  len;
  483. {
  484.     int i;
  485.  
  486.     for (i = 1; i < len && *from; i++)
  487.         *to++ = *from++;
  488.     *to = NULL_CHAR;
  489. }
  490.  
  491. void
  492. strlower(s)
  493. char *s;
  494. {
  495.     while (*s)
  496.     {
  497.         *s = (char) tolower(*s);
  498.         s++;
  499.     }
  500. }
  501.  
  502. void
  503. strupper(s)
  504. char *s;
  505. {
  506.     while (*s)
  507.     {
  508.         *s = (char) toupper(*s);
  509.         s++;
  510.     }
  511. }
  512.  
  513. voidp *
  514. malloc_msg(size)
  515. int size;
  516. {
  517.     char *p;
  518.  
  519.     if ((p = (char *)xmalloc(size)) == NULL)
  520.         error(M_NOMEMORY, "");
  521.     return (voidp *)p;
  522. }
  523.  
  524. static uint
  525. get_word()
  526. {
  527.     uint b0, b1;
  528.  
  529.     b0 = get_byte();
  530.     b1 = get_byte();
  531.     return (b1 << 8) + b0;
  532. }
  533.  
  534. static ulong
  535. get_longword()
  536. {
  537.     ulong b0, b1, b2, b3;
  538.  
  539.     b0 = get_byte();
  540.     b1 = get_byte();
  541.     b2 = get_byte();
  542.     b3 = get_byte();
  543.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  544. }
  545.  
  546. static long
  547. find_header(fd)
  548. FILE *fd;
  549. {
  550.     long arcpos, lastpos;
  551.     int c;
  552.  
  553.     arcpos = file_tell(fd);
  554.     file_seek(fd, 0L, SEEK_END);
  555.     lastpos = file_tell(fd) - 2;
  556.     if (lastpos > MAXSFX)
  557.         lastpos = MAXSFX;
  558.     for ( ; arcpos < lastpos; arcpos++)
  559.     {
  560.         file_seek(fd, arcpos, SEEK_SET);
  561.         c = fget_byte(fd);
  562.         while (arcpos < lastpos)
  563.         {
  564.             if (c != HEADER_ID_LO)  /* low order first */
  565.                 c = fget_byte(fd);
  566.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  567.                 break;
  568.             arcpos++;
  569.         }
  570.         if (arcpos >= lastpos)
  571.             break;
  572.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  573.         {
  574.             crc = CRC_MASK;
  575.             fread_crc(header, (int) headersize, fd);
  576.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  577.             {
  578.                 file_seek(fd, arcpos, SEEK_SET);
  579.                 return arcpos;
  580.             }
  581.         }
  582.     }
  583.     return -1;          /* could not find a valid header */
  584. }
  585.  
  586. static int
  587. read_header(first, fd, name)
  588. int  first;
  589. FILE *fd;
  590. char *name;
  591. {
  592.     ushort extheadersize, header_id;
  593.  
  594.     header_id = fget_word(fd);
  595.     if (header_id != HEADER_ID)
  596.     {
  597.         if (first)
  598.             error(M_NOTARJ, name);
  599.         else
  600.             error(M_BADHEADR, "");
  601.     }
  602.  
  603.     headersize = fget_word(fd);
  604.     if (headersize == 0)
  605.         return 0;               /* end of archive */
  606.     if (headersize > HEADERSIZE_MAX)
  607.         error(M_BADHEADR, "");
  608.  
  609.     crc = CRC_MASK;
  610.     fread_crc(header, (int) headersize, fd);
  611.     header_crc = fget_crc(fd);
  612.     if ((crc ^ CRC_MASK) != header_crc)
  613.         error(M_HEADRCRC, "");
  614.  
  615.     setup_get(header);
  616.     first_hdr_size = get_byte();
  617.     arj_nbr = get_byte();
  618.     arj_x_nbr = get_byte();
  619.     host_os = get_byte();
  620.     arj_flags = get_byte();
  621.     method = get_byte();
  622.     file_type = get_byte();
  623.     (void)get_byte();
  624.     time_stamp = get_longword();
  625.     compsize = get_longword();
  626.     origsize = get_longword();
  627.     file_crc = get_crc();
  628.     entry_pos = get_word();
  629.     file_mode = get_word();
  630.     host_data = get_word();
  631.  
  632.     hdr_filename = (char *)&header[first_hdr_size];
  633.     strncopy(filename, hdr_filename, sizeof(filename));
  634.     if (host_os != OS)
  635.         strparity((uchar *)filename);
  636.     if ((arj_flags & PATHSYM_FLAG) != 0)
  637.         decode_path(filename);
  638.  
  639.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  640.     strncopy(comment, hdr_comment, sizeof(comment));
  641.     if (host_os != OS)
  642.         strparity((uchar *)comment);
  643.  
  644.     /* if extheadersize == 0 then no CRC */
  645.     /* otherwise read extheader data and read 4 bytes for CRC */
  646.  
  647.     while ((extheadersize = fget_word(fd)) != 0)
  648.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  649.  
  650.     return 1;                   /* success */
  651. }
  652.  
  653. static void
  654. skip()
  655. {
  656.     file_seek(arcfile, compsize, SEEK_CUR);
  657. }
  658.  
  659. static void
  660. unstore()
  661. {
  662.     int n;
  663.     long pos;
  664.     char *buffer;
  665.  
  666.     buffer = (char *)malloc_msg(BUFFERSIZE);
  667.     pos = file_tell(arcfile);
  668.     disp_clock();
  669.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  670.     n = compsize > (long)n ? n : (int)compsize;
  671.     while (compsize > 0)
  672.     {
  673.         if (file_read(buffer, 1, n, arcfile) != n)
  674.             error(M_CANTREAD, "");
  675.         disp_clock();
  676.         compsize -= n;
  677.         fwrite_txt_crc((uchar *)buffer, n);
  678.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  679.     }
  680.     free(buffer);
  681. }
  682.  
  683. static int
  684. check_flags()
  685. {
  686.     if (arj_x_nbr > ARJ_X_VERSION)
  687.     {
  688.         printf(M_UNKNVERS, arj_x_nbr);
  689.         printf(M_SKIPPED, filename);
  690.         skip();
  691.         return -1;
  692.     }
  693.     if ((arj_flags & GARBLE_FLAG) != 0)
  694.     {
  695.         printf(M_ENCRYPT);
  696.         printf(M_SKIPPED, filename);
  697.         skip();
  698.         return -1;
  699.     }
  700.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  701.     {
  702.         printf(M_UNKNMETH, method);
  703.         printf(M_SKIPPED, filename);
  704.         skip();
  705.         return -1;
  706.     }
  707.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  708.     {
  709.         printf(M_UNKNTYPE, file_type);
  710.         printf(M_SKIPPED, filename);
  711.         skip();
  712.         return -1;
  713.     }
  714.     return 0;
  715. }
  716.  
  717. static int
  718. extract()
  719. {
  720.     char name[FNAME_MAX];
  721.  
  722.     if (check_flags())
  723.     {
  724.         error_count++;
  725.         return 0;
  726.     }
  727.  
  728.     no_output = 0;
  729.     if (command == 'E')
  730.         strcpy(name, &filename[entry_pos]);
  731.     else
  732.     {
  733.         strcpy(name, DEFAULT_DIR);
  734.         strcat(name, filename);
  735.     }
  736.  
  737.     if (host_os != OS)
  738.         default_case_path(name);
  739.  
  740.     if (file_exists(name))
  741.     {
  742.         printf(M_FEXISTS, name);
  743.         printf(M_SKIPPED, name);
  744.         skip();
  745.         error_count++;
  746.         return 0;
  747.     }
  748.     outfile = file_open(name, writemode[file_type & 1]);
  749.     if (outfile == NULL)
  750.     {
  751.         printf(M_CANTOPEN, name);
  752.         putchar('\n');
  753.         skip();
  754.         error_count++;
  755.         return 0;
  756.     }
  757.     printf(M_EXTRACT, name);
  758.     if (host_os != OS && file_type == BINARY_TYPE)
  759.         printf(M_DIFFHOST);
  760.     printf("  ");
  761.  
  762.     crc = CRC_MASK;
  763.  
  764.     if (method == 0)
  765.         unstore();
  766.     else if (method == 1 || method == 2 || method == 3)
  767.         decode();
  768.     else if (method == 4)
  769.         decode_f();
  770.     fclose(outfile);
  771.  
  772.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  773.  
  774.     if ((crc ^ CRC_MASK) == file_crc)
  775.         printf(M_CRCOK);
  776.     else
  777.     {
  778.         printf(M_CRCERROR);
  779.         error_count++;
  780.     }
  781.     return 1;
  782. }
  783.  
  784. static int
  785. test()
  786. {
  787.     if (check_flags())
  788.         return 0;
  789.  
  790.     no_output = 1;
  791.     printf(M_TESTING, filename);
  792.     printf("  ");
  793.  
  794.     crc = CRC_MASK;
  795.  
  796.     if (method == 0)
  797.         unstore();
  798.     else if (method == 1 || method == 2 || method == 3)
  799.         decode();
  800.     else if (method == 4)
  801.         decode_f();
  802.  
  803.     if ((crc ^ CRC_MASK) == file_crc)
  804.         printf(M_CRCOK);
  805.     else
  806.     {
  807.         printf(M_CRCERROR);
  808.         error_count++;
  809.     }
  810.     return 1;
  811. }
  812.  
  813. static uint
  814. ratio(a, b)
  815. long a, b;
  816. {
  817.    int i;
  818.  
  819.    for (i = 0; i < 3; i++)
  820.        if (a <= LONG_MAX / 10)
  821.            a *= 10;
  822.        else
  823.            b /= 10;
  824.    if ((long) (a + (b >> 1)) < a)
  825.    {
  826.        a >>= 1;
  827.        b >>= 1;
  828.    }
  829.    if (b == 0)
  830.        return 0;
  831.    return (uint) ((a + (b >> 1)) / b);
  832. }
  833.  
  834. static void
  835. list_start()
  836. {
  837.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  838.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  839. }
  840.  
  841. static void
  842. list_arc(count)
  843. int count;
  844. {
  845.     uint r;
  846.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  847.     char date_str[20], fmode_str[10];
  848.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  849.     static char pthf[2] = { ' ', '+' };
  850.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  851.     static char volf[2] = { ' ', 'V' };
  852.     static char extf[2] = { ' ', 'X' };
  853.     static char bckf[2] = { ' ', '*' };
  854.  
  855.     if (count == 0)
  856.         list_start();
  857.  
  858.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  859.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  860.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  861.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  862.     path_mode   = (entry_pos > 0);
  863.     r = ratio(compsize, origsize);
  864.     torigsize += origsize;
  865.     tcompsize += compsize;
  866.     ftype = file_type;
  867.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  868.             ftype != LABEL_TYPE)
  869.         ftype = 3;
  870.     get_date_str(date_str, time_stamp);
  871.     strcpy(fmode_str, "    ");
  872.     if (host_os == OS)
  873.         get_mode_str(fmode_str, (uint) file_mode);
  874.     if (strlen(&filename[entry_pos]) > 12)
  875.         printf("%-12s\n             ", &filename[entry_pos]);
  876.     else
  877.         printf("%-12s ", &filename[entry_pos]);
  878.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  879.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  880.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  881.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  882. }
  883.  
  884. static void
  885. execute_cmd()
  886. {
  887.     int file_count;
  888.     char date_str[22];
  889.     uint r;
  890.  
  891.     first_hdr_pos = 0;
  892.     time_stamp = 0;
  893.     first_hdr_size = FIRST_HDR_SIZE;
  894.  
  895.     arcfile = fopen_msg(arc_name, "rb");
  896.  
  897.     printf(M_PROCARC, arc_name);
  898.  
  899.     first_hdr_pos = find_header(arcfile);
  900.     if (first_hdr_pos < 0)
  901.         error(M_NOTARJ, arc_name);
  902.     file_seek(arcfile, first_hdr_pos, SEEK_SET);
  903.     if (!read_header(1, arcfile, arc_name))
  904.         error(M_BADCOMNT, "");
  905.     get_date_str(date_str, time_stamp);
  906.     printf(M_ARCDATE, date_str);
  907.     if (arj_nbr >= ARJ_M_VERSION)
  908.     {
  909.         get_date_str(date_str, (ulong) compsize);
  910.         printf(M_ARCDATEM, date_str);
  911.     }
  912.     printf("\n");
  913.  
  914.     file_count = 0;
  915.     while (read_header(0, arcfile, arc_name))
  916.     {
  917.         switch (command)
  918.         {
  919.         case 'E':
  920.         case 'X':
  921.             if (extract())
  922.                 file_count++;
  923.             break;
  924.         case 'L':
  925.             list_arc(file_count++);
  926.             skip();
  927.             break;
  928.         case 'T':
  929.             if (test())
  930.                 file_count++;
  931.             break;
  932.         }
  933.     }
  934.  
  935.     if (command == 'L')
  936.     {
  937.         printf("------------ ---------- ---------- ----- -----------------\n");
  938.         r = ratio(tcompsize, torigsize);
  939.         printf(" %5d files %10ld %10ld %u.%03u %s\n",
  940.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  941.     }
  942.     else
  943.         printf(M_NBRFILES, file_count);
  944.  
  945.     fclose(arcfile);
  946. }
  947.  
  948. static void
  949. help()
  950. {
  951.     int i;
  952.  
  953.     for (i = 0; M_USAGE[i] != NULL; i++)
  954.         printf(M_USAGE[i]);
  955. }
  956.  
  957. int
  958. main(argc, argv)
  959. int  argc;
  960. char *argv[];
  961. {
  962.     int i, j, lastc;
  963.     char *arc_p;
  964.  
  965. #ifdef THINK_C
  966.     argc = ccommand(&argv);
  967. #endif
  968.  
  969.     printf(M_VERSION);
  970.  
  971.     if (argc == 1)
  972.     {
  973.         help();
  974.         return EXIT_SUCCESS;
  975.     }
  976.     else if (argc == 2)
  977.     {
  978.         command = 'L';
  979.         arc_p = argv[1];
  980.     }
  981.     else if (argc == 3)
  982.     {
  983.         if (strlen(argv[1]) > 1)
  984.             error(M_BADCOMND, argv[1]);
  985.         command = toupper(*argv[1]);
  986.         if (strchr("ELTX", command) == NULL)
  987.             error(M_BADCOMND, argv[1]);
  988.         arc_p = argv[2];
  989.     }
  990.     else
  991.     {
  992.         help();
  993.         return EXIT_FAILURE;
  994.     }
  995.  
  996.     strncopy(arc_name, arc_p, FNAME_MAX);
  997.     case_path(arc_name);
  998.     i = strlen(arc_name);
  999.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  1000.     lastc = arc_name[i - 1];
  1001.     if (lastc == ARJ_DOT)
  1002.         arc_name[i - 1] = NULL_CHAR;
  1003.     else if (strchr(&arc_name[j], ARJ_DOT) == NULL)
  1004.         strcat(arc_name, M_SUFFIX);
  1005.  
  1006.     make_crctable();
  1007.  
  1008.     error_count = 0;
  1009.     clock_inx = 0;
  1010.     arcfile = NULL;
  1011.     outfile = NULL;
  1012.  
  1013.     execute_cmd();
  1014.  
  1015.     if (error_count > 0)
  1016.         error(M_ERRORCNT, "");
  1017.  
  1018.     return EXIT_SUCCESS;
  1019. }
  1020.  
  1021. /* end UNARJ.C */
  1022.